import { login, logout, getInfo, unlock, getOrganizeId } from "@/api/user";
import { getDictDataRedis } from "@/api/systemData/dictionary";
import {
  setLock,
  getLock,
  removeLock,
  getToken,
  setToken,
  removeToken,
  getDict,
  setDict,
  removeDict,
  getAdminOrganizeId,
  removeAdminOrganizeId,
  setAdminOrganizeId,
} from "@/utils/auth";
import { resetRouter } from "@/router";
import qs from "qs";
import md5 from "js-md5";
const define = require("@/utils/define");

const state = {
  token: getToken(),
  isLock: getLock() || 0,
  menuList: [],
  userInfo: {},
  permissionList: [],
  loginLoading: false,
  socket: "",
  dict: getDict(),
  organizeId: getAdminOrganizeId(),
};

const getters = {
  getDict({ dict }) {
    // ByDictType
    return (type = "typeCode", val, isGetValue) => {
      const targetDict = dict[type];
      if (!targetDict) {
        return;
      }
      if (typeof val !== "undefined" && val !== null && val !== "") {
        const target = targetDict.find((item) => item.id === val.toString());
        if (!target) {
          return "";
        }
        return target.dataName;
      }
      // 防止出现不存在的字典值
      if (isGetValue) {
        return "";
      }
      return dict[type];
    };
  },
};

const mutations = {
  SET_SOCKET: (state, socket) => {
    state.socket = socket;
  },
  SET_TOKEN: (state, token) => {
    state.token = token;
  },
  SET_ADMINORGANIZEID: (state, organizeId) => {
    state.organizeId = organizeId;
  },
  SET_MENULIST: (state, menuList) => {
    state.menuList = menuList;
  },
  SET_USERINFO: (state, userInfo) => {
    state.userInfo = userInfo;
  },
  SET_USERINFO_HEADICON: (state, headIcon) => {
    state.userInfo.headIcon = headIcon;
  },
  SET_USERINFO_USERNAME: (state, userName) => {
    state.userInfo.userName = userName;
  },
  SET_USERINFO_PORTALID: (state, portalId) => {
    state.userInfo.portalId = portalId;
  },
  SET_PERMISSION_LIST: (state, permissionList) => {
    state.permissionList = permissionList;
  },
  SET_LOCK: (state, isLock) => {
    state.isLock = isLock;
  },
  SET_LOGIN_LOADING: (state, loginLoading) => {
    state.loginLoading = loginLoading;
  },
  SET_DICT: (state, dict) => {
    state.dict = dict;
  },
};

const actions = {
  // 锁屏
  setLock({ commit }) {
    commit("SET_LOCK", 1);
    setLock(1);
  },
  clearLock({ commit }) {
    commit("SET_LOCK", "");
    removeLock();
  },
  unlock({ dispatch }, userInfo) {
    const { account, password } = userInfo;
    return new Promise((resolve, reject) => {
      unlock({ account: account.trim(), password: md5(password) })
        .then((response) => {
          dispatch("clearLock");
          resolve();
        })
        .catch((error) => {
          reject(error);
        });
    });
  },
  // user login
  login({ commit }, userInfo) {
    const { account, password } = userInfo;
    return new Promise((resolve, reject) => {
      login(
        qs.stringify({
          account: account.trim(),
          password: md5(password),
          client_id: "admin",
          client_secret: "123456",
          scope: "all",
          grant_type: "password",
        })
      )
        .then((response) => {
          const { data } = response;
          const layoutList = ["classic", "functional", "plain"];
          let layoutType =
            data.theme && layoutList.indexOf(data.theme) > -1
              ? data.theme
              : "classic";
          commit("SET_TOKEN", data.token);
          commit(
            "settings/CHANGE_SETTING",
            { key: "layoutType", value: layoutType },
            { root: true }
          );
          setToken(data.token);
          // 请求字典码
          getDictDataRedis()
            .then((res) => {
              const list = {};
              res.data.forEach((item) => {
                !list[item.typeCode] && (list[item.typeCode] = []);
                list[item.typeCode].push(item);
              });
              commit("SET_DICT", list);
              setDict(list);
            })
            .catch((error) => {});
          resolve();
        })
        .catch((error) => {
          reject(error);
        });
    });
  },

  // 获取用户信息
  getInfo({ commit }) {
    return new Promise((resolve, reject) => {
      getInfo()
        .then(async (response) => {
          const { data } = response;
          if (!data) reject("验证失败，请重新登录。");
          let { menuList, userInfo, permissionList } = data;
          if (!menuList.length) {
            reject("您的权限不足，请联系管理员");
            return false;
          }
          // 获取组织
          const result = await getOrganizeId();

          if (result.data.length == 0) {
            reject("您的权限不足，请联系管理员");
          }
          let organizeId = "";
          const adminInfo = result.data.find(
            (item) => item.id == "29eab438d531474f9159e60fbf5072ea"
          );
          if (adminInfo && adminInfo[0]) {
            organizeId = adminInfo[0].id;
          } else {
            organizeId = result.data[0].id;
          }
          commit("SET_ADMINORGANIZEID", organizeId);
          setAdminOrganizeId(organizeId);
          let routerList = [];

          function setData(list) {
            for (let i = 0; i < list.length; i++) {
              const e = list[i];
              let name = e.enCode.replace(/\./g, "-");
              e.vueName = name;
              if (e.type == 1) {
                e.path = "/" + e.enCode;
                if (e.hasChildren && e.children.length) {
                  setData(e.children);
                }
              }
              if (e.type == 2) {
                let path = e.urlAddress;
                if (path.indexOf("?") > -1) path = path.split("?")[0];
                e.path = "/" + e.urlAddress;
                let newObj = {
                  path: "/" + path,
                  component: (resolve) => require([`@/views/${path}`], resolve),
                  name: name,
                  meta: {
                    title: name,
                    icon: e.icon,
                    zhTitle: e.fullName,
                    modelId: e.id,
                  },
                };
                routerList.push(newObj);
              }
              // 功能、字典、报表、门户
              if ([3, 4, 5, 8].indexOf(e.type) > -1) {
                let propertyJson = e.propertyJson
                    ? JSON.parse(e.propertyJson)
                    : null,
                  relationId = "",
                  isTree = 0,
                  componentUrl = "";
                if (propertyJson) {
                  relationId = propertyJson.moduleId || "";
                  isTree = propertyJson.isTree || 0;
                }
                if (e.type == 3) {
                  componentUrl = "dynamicModel";
                } else if (e.type == 4) {
                  componentUrl = "dynamicDictionary";
                } else if (e.type == 5) {
                  componentUrl = "dynamicDataReport";
                } else {
                  componentUrl = "dynamicPortal";
                }
                e.path = "/" + e.urlAddress;
                let newObj = {
                  path: "/" + e.urlAddress,
                  component: (resolve) =>
                    require([`@/views/basic/${componentUrl}`], resolve),
                  name: e.enCode,
                  meta: {
                    title: name,
                    icon: e.icon,
                    zhTitle: e.fullName,
                    modelId: e.id,
                    relationId,
                    isTree,
                  },
                };
                routerList.push(newObj);
              }
              // 大屏
              if (e.type == 6) {
                let propertyJson = e.propertyJson
                    ? JSON.parse(e.propertyJson)
                    : null,
                  moduleId = "";
                if (propertyJson) moduleId = propertyJson.moduleId || "";
                e.path = `${define.dataV}/view/${moduleId}?token=${getToken()}`;
              }
              // 外链
              if (e.type == 7) {
                if (e.linkTarget === "_self") {
                  e.path = "/" + e.enCode;
                  let newObj = {
                    path: "/" + e.enCode,
                    component: (resolve) =>
                      require([`@/views/basic/externalLink`], resolve),
                    name: e.enCode,
                    meta: {
                      title: name,
                      icon: e.icon,
                      zhTitle: e.fullName,
                      modelId: e.id,
                      urlAddress: e.urlAddress,
                    },
                  };
                  routerList.push(newObj);
                } else {
                  const path = e.urlAddress
                    .replace(/\${dataV}/g, define.dataV)
                    .replace(/\${jadpToken}/g, getToken());
                  e.path = path;
                }
              }
            }
          }
          setData(menuList);
          commit("SET_MENULIST", menuList);
          commit("SET_USERINFO", userInfo);
          commit("SET_PERMISSION_LIST", permissionList);
          resolve(routerList);
        })
        .catch((error) => {
          reject(error);
        });
    });
  },

  // user logout
  logout({ commit, state, dispatch }) {
    return new Promise((resolve, reject) => {
      logout(state.token)
        .then(() => {
          init(commit);
          commit("SET_DICT", {});
          resetRouter();
          // reset visited views and cached views
          dispatch("tagsView/delAllViews", null, { root: true });
          resolve();
        })
        .catch((error) => {
          reject(error);
        });
    });
  },
  // remove token
  resetToken({ commit }) {
    return new Promise((resolve) => {
      init(commit);
      commit("SET_LOGIN_LOADING", false);
      resolve();
    });
  },
};

const init = (commit) => {
  removeToken();
  removeAdminOrganizeId();
  removeDict();
  commit("SET_TOKEN", "");
  commit("SET_ADMINORGANIZEID", "");
  commit("SET_MENULIST", []);
  commit("SET_USERINFO", {});
  commit("SET_PERMISSION_LIST", []);
};

export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions,
};
